package com.qb.ems.service.impl;

import com.qb.ems.entity.User;
import com.qb.ems.entity.UserDao;
import com.qb.ems.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

//    @Transactional(transactionManager = "jtaTransactionManager")
    @Override
    public void addUser(User user) {

        userDao.save(user);
    }

    public Optional<User> findUserById(Integer id) {
        return userDao.findById(id);
    }

    public List<User> findAllUserById(Integer id) {
        return userDao.findAllById(new ArrayList<>(id));
    }

    public void delUser(Integer id) {
        userDao.deleteById(id);
    }

    @Override
    public Page<User> getUserWithPage(Integer page, Integer size) {
        if (page < 1){
            page = 1;
        }
        Pageable pageRequest = PageRequest.of(page - 1, size);
        return userDao.findAll(pageRequest);
    }
}
